package countdownlatch;

import java.util.concurrent.CountDownLatch;

/**
 * Created by 5406160@qq.com on LiBoWen 李博闻.
 *
 * @ClassName : Demo
 * @Author : 54061
 * @Date: 2022/2/17 9:27
 */
public class Demo {
    public static void main(String[] args) {
        //创建一个计数对象
    }
}
//孩子线程1
class ChildThread1 extends Thread{
    private CountDownLatch countDownLatch;
    public ChildThread1(CountDownLatch countDownLatch,String name){
        super(name);
        this.countDownLatch = countDownLatch;

    }

    @Override
    public void run() {
        //吃饺子
        for (int i = 0; i < 10; i++) {
            System.out.println(getName()+"在吃第"+i+"个饺子");
        }
        //吃完说一声
        //每一次countDown方法的时候 就让计数器减一
        countDownLatch.countDown();
    }
}
//孩子线程2
class ChildThread2 extends Thread{
    private CountDownLatch countDownLatch;
    public ChildThread2(CountDownLatch countDownLatch,String name){
        super(name);
        this.countDownLatch = countDownLatch;

    }

    @Override
    public void run() {
        //吃饺子
        for (int i= 1 ; i<=20;i++){
            System.out.println(getName()+"在吃第"+i+"个饺子");
        }
        //吃完说一声
        //每一次countDown方法的时候就让计数器-1
        countDownLatch.countDown();
    }
}
//孩子线程3
class ChildThread3 extends Thread{
    private CountDownLatch countDownLatch;
    public ChildThread3(CountDownLatch countDownLatch,String name){
        super(name);
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        //吃饺子
        for (int i = 0; i < 15; i++) {
            System.out.println(getName()+"在吃第"+i+"个饺子");

        }
        //吃完说一声
        //每一次countDown方法的时候,就让计数器-1
        countDownLatch.countDown();
    }
}
//妈妈线程
class MotherThead extends Thread{
    private CountDownLatch countDownLatch;
    public MotherThead(CountDownLatch countDownLatch,String name){
        super(name);
        this.countDownLatch = countDownLatch;

    }

    @Override
    public void run() {
        //等待
        //当计数器变成0的时候,会自动唤醒这里等待线程
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //收拾碗筷
        System.out.println("妈妈在收拾碗筷");
    }
}